<template>
  <uv-popup
    ref="popupRef"
    round="8"
    :show="show"
    mode="center"
    closeable
    @change="e=> emit('update:show', e.show)"
  >
    <view class="p-15">
      <view class="text-16 mb-12">备注</view>
      <view class="cu-textarea" style="width: 560rpx;">
        <textarea v-model="content" auto-focus maxlength="500" />
      </view>
      <view class="flex mt-20">
        <view class="flex-1"><uv-button text="取消" @click="close()" /></view>
        <view class="flex-1 ml-12"><uv-button type="primary" text="保存" @click="save" /></view>
      </view>
    </view>
  </uv-popup>
</template>

<script setup>
import { ref, defineProps, defineEmits, watch } from 'vue'
import $ from '@/sheep'
const props = defineProps({
  show: Boolean,
  text: String,
  current: Number
})
const emit = defineEmits(['update:show', 'done'])
const content = ref('')
const popupRef = ref(null)

watch(() => props.show, val => {
  if (val) {
    open()
  } else {
    close()
  }
})

function open() {
  popupRef.value.open()
  content.value = props.text
}

function close() {
  popupRef.value.close()
}

function save() {
  const data = {
    id: props.current,
    remark: content.value
  }
  $.post('/Store/remarkCart', data).then(res => {
    $.utils.toast(res.msg)
    if (res.success) {
      emit('done', data)
      close()
    }
  })
}
</script>
